<?php
namespace app\common\controller;
use \think\Request;

trait BaseLog {

	// protected $autoWriteUserId;
	// protected $requireUserId;

	public function getLog(){
		$baseModel=model($this->modelname);
		$data = $baseModel->getDataById($this->param('id'));

		if ($data){
			$query=$data->logs();
			$page=$this->param('page',1);
			$limit=$this->param('limit',10);
			if ($page&&$limit)
				$query=$query->page($page,$limit);

			return resultArray(['data' => $query->select()]);

		} else {
			return resultArray(['error' => "暂无此资源"]);
		}
	}

	public function logOne(){
		$baseModel=model($this->modelname);
		$data = $baseModel::get($this->param('id',0));
		$param=[];
		if (!$data)
			return resultArray(['error' => "必须指定资源"]);

		if ($this->autoWriteUserId){
			if ($u=$GLOBALS['userInfo']){
				$param['user_id']=$u->id;
			} else {
				return resultArray(['error' => "身份认证失败"]);
			}
		} else {
			if (isset($this->param['user_id'])){
				$param['user_id']=$this->param['user_id'];
			} else {
				return resultArray(['error' => "必须指明操作员"]);
			}
		}

		$param['logtext']=$this->param('logtext','');

		if ($data->logOne($param)) {
			return resultArray(['error' => "添加log成功"]);
		} else {
			return resultArray(['error' => "添加log失败:".$data->getError()]);
		}
	}

}

?>